<!--
 * @Description: 业务管理系统
 * @Author: fxg
 * @Date: 2020-06-29 11:18:28
 * @LastEditors: wangwangwang
 * @LastEditTime: 2021-03-19 11:30:23
-->
<template>
  <div class="pages_content partTime">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main>
        <el-container>
          <el-header style="background: #FFFFFF;">
            <el-button-group>
              <el-button :class="currrenIndex == 1 ? 'active' : 'botton_top'" @click="curRrenIndex(1)">用户快捷配置</el-button>
              <el-button :class="currrenIndex == 2 ? 'active' : 'botton_top'" @click="curRrenIndex(2)">用户配置</el-button>
            </el-button-group>
          </el-header>
          <!-- 用户快捷配置 -->
          <el-main v-if="currrenIndex == 1" class="info">
            <div class="left">
              <div class="nav">用户</div>
              <div class="left-info">
                <div class="search">
                  <el-input v-model="keyword" size="mini" placeholder="请输入内容" suffix-icon="el-icon-search" clearable></el-input>
                </div>
                <div v-for="(item, index) in newuserarr" :key="index" class="user" @click="chooseUser(item)">
                  <span :class="item.id == userindex ? 'user-active' : ''">{{ item.name }}</span>
                </div>
              </div>
            </div>
            <div class="center"></div>
            <div class="right">
              <div class="nav">兼职岗位</div>
              <div class="right-info">
                <div class="right-info-left">
                  <el-tree node-key="id" :data="orgarr" class="tree-line"  :indent="0"  :props="defaultProps" default-expand-all :expand-on-click-node="false" @node-click="clickOrg">
                    <!-- <span slot-scope="{ node, data }" class="custom-tree-node">
                      <div v-if="data.imgIcon" style="display: inline-block;width:17px;height:26px;padding:8px 3px 0 0">
                        <img style="width:14px;height:14px" :src="data.imgIcon" />
                      </div>
                      <span :style="oldorgarr.indexOf(data.id)!==-1?'color:red; font-weight: 1000;':''">{{ node.label }}</span>
                    </span> -->
                    <span slot-scope="{ node ,data}" class="custom-tree-node">
                      <el-radio-group v-model="orgindex" @change="clickOrg(data)">
                        <el-radio :label="data.id">
                          <span :style="oldorgarr.indexOf(data.id)!==-1?'color:red; font-weight: 1000;':''">{{ node.label }}</span>
                        </el-radio>
                      </el-radio-group>
                    </span>
                  </el-tree>
                </div>
                <div class="right-info-center"></div>
                <div class="right-info-right">
                  <!-- <div v-for="(item, index) in gwarr" :key="index" class="user" @click="sumbit(item)">
                    <span :class="oldgwarr.indexOf(item.id)!==-1? 'user-active' : ''">
                      <i class="icon iconfont icontree_icon_jobs"></i>
                      {{ item.name }}
                    </span>
                  </div> -->
                  <el-checkbox v-for="(item, index) in gwarr" :key="index" class="CheckboxBlock" :checked="oldgwarr.indexOf(item.id)!= -1 ? true : false" :label="item.id" :class="oldgwarr.indexOf(item.id)!==-1? 'user-active' : ''" @change="sumbit(item)"><i class="icon iconfont icontree_icon_jobs"></i>{{item.name}}</el-checkbox>
                </div>
              </div>
            </div>
          </el-main>
          <!-- 用户配置 -->
          <el-main v-if="currrenIndex == 2" class="info">
            <div class="right">
              <div class="nav">兼职岗位</div>
              <div class="right-info">
                <div class="right-info-left">
                  <el-tree node-key="id" :data="orgarr" class="tree-line"  :indent="0"  :props="defaultProps" default-expand-all :expand-on-click-node="false" @node-click="clickOrg1">
                    <span slot-scope="{ node, data }" class="custom-tree-node">
                      <div v-if="data.imgIcon" style="display: inline-block;width:17px;height:26px;padding:8px 3px 0 0">
                        <img style="width:14px;height:14px" :src="data.imgIcon" />
                      </div>
                      <span>{{ node.label }}</span>
                    </span>
                  </el-tree>
                </div>
                <div class="right-info-center"></div>
                <div class="right-info-right">
                  <div v-for="(item, index1) in gwarr" :key="index1" class="user">
                    <span :class="gwindex==item.id? 'user-active' : ''" :style="oldgwarr.indexOf(item.id)!==-1?'color:red; font-weight: 1000;':''" @click="sumbit1(item)">
                      <el-radio v-model="gwindex" class="radioMarginRight0" :label="item.id">
                        <span :style="oldgwarr.indexOf(item.id)!==-1?'color:red; font-weight: 1000;':''">{{''}}</span>
                      </el-radio>
                      <i class="icon iconfont icontree_icon_jobs"></i>
                      {{ item.name }}
                    </span>
                  </div>
                </div>
              </div>
            </div>
            <div class="center"></div>
            <div class="left">
              <div class="nav">用户</div>
              <div class="left-info">
                <div class="search">
                  <el-input v-model="keyword" size="mini" placeholder="请输入内容" suffix-icon="el-icon-search" clearable></el-input>
                </div>
                <!-- <div v-for="(item, index) in newuserarr" :key="index" class="user" @click="chooseUser1(item)">
                  <i class="icon iconfont icontree_icon_jobs"></i>
                  <span :class="olduserarr.indexOf(item.id)!==-1? 'user-active' : ''">{{ item.name }}</span>
                </div> -->
                <el-checkbox v-for="(item, index) in newuserarr" :key="index" class="CheckboxBlock" :checked="olduserarr.indexOf(item.id)!= -1 ? true : false" :label="item.userId" :class="olduserarr.indexOf(item.id)!==-1? 'user-active' : ''" @change="chooseUser1(item)"><i class="icon iconfont icontree_icon_jobs"></i>{{item.name}}</el-checkbox>
              </div>
            </div>
          </el-main>
        </el-container>
      </el-main>
    </el-container>
  </div>
</template>
<script>
import breaDcrumb from "@/components/breadcrumb";
import { queryPage } from "@/api/basedata/user";
import { queryPage as organgequeryPage } from "@/api/basedata/organization.js";
import { queryPage as gwqueryPage } from "@/api/basedata/jobs.js";
import { saveOrdelete, getList } from "@/api/basedata/partTime.js";
import { formatTreeData } from "@/utils/utils";
import { mapGetters } from "vuex";
import styles from "@/app.scss";
export default {
  components: {
    breaDcrumb,
  },
  data() {
    return {
      bg_color: styles.bg_color,
      binding: [],
      oldgwarr: [],
      oldorgarr: [],
      olduserarr: [],
      gwarr: [],
      orgarr: [],
      keyword: "",
      newuserarr: [],
      userarr: [],
      currrenIndex: 1,
      userindex: null,
      gwindex: null,
      orgindex: null,
      defaultProps: {
        children: "children",
        label: "name",
      },
    };
  },
  created() {
    this.getall();
  },
  watch: {
    keyword() {
      this.newuserarr = new Array();
      if (this.keyword == "") {
        this.newuserarr = this.userarr;
      } else {
        this.userarr.forEach((item) => {
          if (item.name.indexOf(this.keyword) !== -1) {
            this.newuserarr.push(item);
          }
        });
      }
    },
  },
  methods: {
    curRrenIndex(num) {
      this.keyword = "";
      this.currrenIndex = num;
      this.orgindex = null;
      this.userindex = null;
      this.gwindex = null;
      this.binding = [];
      this.oldgwarr = [];
      this.oldorgarr = [];
      this.olduserarr = [];
    },
    /*用户配置*/
    getbinding1() {
      getList({ id: this.orgindex, type: "type_organization" }).then((res) => {
        this.binding = res.data;
        this.oldgwarr = res.data.map((res) => {
          return res.stationsId;
        });
      });
    },
    clickOrg1(item) {
      this.orgindex = item.id;
      this.getbinding1();
      this.userarr = [];
      this.olduserarr = [];
    },
    sumbit1(item) {
      this.gwindex = item.id;
      if (!this.orgindex) {
        this.$message({
          message: "警告哦，请先选择组织",
          type: "warning",
        });
        return
      }
      let key = this.binding.filter((res) => res.stationsId == item.id);
      console.log(key);
      this.olduserarr = key.map((res) => {
        return res.userId;
      });
      this.newuserarr = []
      queryPage({ page: 1, pageSize: 10000000, enable: 1 }).then((res) => {
        this.userarr = res.data.data;
        this.newuserarr = this.userarr;
        console.log(JSON.parse(JSON.stringify(this.newuserarr)))
      });
    },
    chooseUser1(item) {
      if (!this.gwindex) {
        this.$message({
          message: "警告哦，请先选择岗位",
          type: "warning",
        });
        return
      }
      if (this.olduserarr.indexOf(item.id) == -1) {
        saveOrdelete({
          checkType: "check_yes",
          organizationId: this.orgindex,
          userId: item.id,
          stationsId: this.gwindex,
        }).then((res) => {
          if (res.code == 1001) {
            this.olduserarr.push(item.id);
            this.getbinding1();
          }
        });
      } else {
        saveOrdelete({
          checkType: "check_no",
          organizationId: this.orgindex,
          userId: item.id,
          stationsId: this.gwindex,
        }).then((res) => {
          if (res.code == 1001) {
            this.olduserarr.splice(
              this.olduserarr.findIndex((res) => res == item.id),
              1
            );
            this.getbinding1();
          }
        });
      }
    },
    /*用户快捷配置*/
    /*获取绑定关系*/
    getbinding() {
      getList({ id: this.userindex, type: "type_user" }).then((res) => {
        this.binding = res.data;
        this.oldorgarr = res.data.map((res) => {
          return res.organizationId;
        });
      });
    },
    /*选择用户*/
    chooseUser(item) {
      this.userindex = item.id;
      this.getbinding();
      this.oldgwarr = [];
    },
    /*选择组织*/
    clickOrg(item) {
      this.orgindex = item.id;
      if (!this.userindex) {
        this.$message({
          message: "警告哦，请先选择用户",
          type: "warning",
        });
        return
      }
      let key = this.binding.filter((res) => res.organizationId == item.id);
      this.oldgwarr = key.map((res) => {
        return res.stationsId;
      });
      this.gwarr = []
      gwqueryPage({ page: 1, pageSize: 10000000, enable: 1 }).then((res) => {
        this.gwarr = res.data.data;
      });
    },
    /*绑定*/
    sumbit(item) {
      if (!this.orgindex) {
        this.$message({
          message: "警告哦，请先选择组织",
          type: "warning",
        });
        return
      }
      if (this.oldgwarr.indexOf(item.id) == -1) {
        saveOrdelete({
          checkType: "check_yes",
          organizationId: this.orgindex,
          userId: this.userindex,
          stationsId: item.id,
        }).then((res) => {
          if (res.code == 1001) {
            this.oldgwarr.push(item.id);
            this.getbinding();
          }
        });
      } else {
        saveOrdelete({
          checkType: "check_no",
          organizationId: this.orgindex,
          userId: this.userindex,
          stationsId: item.id,
        }).then((res) => {
          if (res.code == 1001) {
            this.oldgwarr.splice(
              this.oldgwarr.findIndex((res) => res == item.id),
              1
            );
            this.getbinding();
          }
        });
      }
    },
    /*获取数据*/
    getall() {
      let params = {
        page: 1,
        pageSize: 10000000,
      };
      queryPage({ ...params, enable: 1 }).then((res) => {
        this.userarr = res.data.data;
        this.newuserarr = this.userarr;
      });
      organgequeryPage({ ...params, showEnable: 0 }).then((res) => {
        if (res.code === 1001) {
          const { data } = res.data;
          this.orgarr = formatTreeData(
            data,
            "id",
            "parentId",
            "children",
            "parent_id"
          );
          console.log(JSON.parse(JSON.stringify(this.orgarr)))
        }
      });
      gwqueryPage({ page: 1, pageSize: 10000000, enable: 1 }).then((res) => {
        this.gwarr = res.data.data;
      });
    },
  },
  computed: {
    ...mapGetters({
      screenWidth: "getscreenWidth",
      screenHeight: "getscreenHeight",
      Collapse: "getsCollapse",
    }),
    style() {
      let num = 0;
      if (this.Collapse) {
        num = this.screenWidth - 124;
      } else {
        num = this.screenWidth - 220;
      }
      return "width:" + num + "px;";
    },
  },
};
</script>
<style>
.is-current > .el-tree-node__content {
  color: var(--bg_color);
}
</style>
<style lang="scss" scoped>
.user-active {
  color: $bg_color !important;
}
.user {
  line-height: 24px;
  font-size: $font_size;
  color: #4b4c4d;
  font-weight: 400;
}
.user:hover {
  cursor: pointer;
}
.right-info {
  display: flex;
  .right-info-left {
    padding: 20px;
    height: 100%;
    width: calc((100% - 1px) / 2);
    overflow: auto;
  }
  .right-info-right {
    padding: 20px;
    height: 100%;
    width: calc((100% - 1px) / 2);
    overflow: auto;
  }
  .right-info-center {
    width: 1px;
    height: 100%;
    background: #e1e5ed;
  }
}

.left-info {
  padding: 20px;
  .search {
    padding-bottom: 15px;
  }
}
.partTime {
  .iconfont {
    font-size: 14px;
    padding: 0 10px;
  }
  .el-main {
    padding: 0;
    flex: 1;
    .el-header {
      height: 96px !important;
      padding: 24px 20px;
    }
  }

  .el-button-group {
    border-radius: 4px;
    overflow: hidden;
    width: 380px;
    height: 48px;

    .el-button {
      width: 190px;
      height: 48px;
      border: 1px solid $bg_color;
      color: $bg_color;
    }

    .active {
      background: $bg_color;
      color: #ffffff;
    }
  }

  .info {
    padding: 0px 20px 10px 20px;
    display: flex;
    .left {
      height: 100%;
      flex: 3;
      border: 1px solid rgba(225, 229, 237, 1);
      border-radius: 4px;
    }

    .center {
      width: 20px;
      height: 100%;
    }

    .right {
      height: 100%;
      flex: 5;
      border: 1px solid rgba(225, 229, 237, 1);
      border-radius: 4px;
    }
  }

  .left {
    .nav {
      padding: 0 20px;
      height: 56px;
      font-weight: bold;
      font-size: calc(#{$font_size} + 2px);
      background: #e1e5ed;
      color: #000000;
      @include discen_al;
    }

    .left-info {
      height: calc(100% - 56px);
      overflow: auto;
    }
  }
  .right {
    .nav {
      padding: 0 20px;
      height: 56px;
      font-weight: bold;
      font-size: calc(#{$font_size} + 2px);
      background: #e1e5ed;
      color: #000000;
      @include discen_al;
    }
    .right-info {
      height: calc(100% - 56px);
      overflow: auto;
    }
  }
}
.right-info-right::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
.right-info-right::-webkit-scrollbar-track {
  background: rgb(239, 239, 239);
  border-radius: 2px;
}
.right-info-right::-webkit-scrollbar-thumb {
  background: #bfbfbf;
  border-radius: 10px;
}
.right-info-right::-webkit-scrollbar-thumb:hover {
  background: $bg_color;
}
.right-info-right::-webkit-scrollbar-corner {
  background: $bg_color;
}
.CheckboxBlock {
  display: block;
}
.right-info-left::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
.right-info-left::-webkit-scrollbar-track {
  background: rgb(239, 239, 239);
  border-radius: 2px;
}
.right-info-left::-webkit-scrollbar-thumb {
  background: #bfbfbf;
  border-radius: 10px;
}
.right-info-left::-webkit-scrollbar-thumb:hover {
  background: $bg_color;
}
.right-info-left::-webkit-scrollbar-corner {
  background: $bg_color;
}
.left-info::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
.left-info::-webkit-scrollbar-track {
  background: rgb(239, 239, 239);
  border-radius: 2px;
}
.left-info::-webkit-scrollbar-thumb {
  background: #bfbfbf;
  border-radius: 10px;
}
.left-info::-webkit-scrollbar-thumb:hover {
  background: $bg_color;
}
.left-info::-webkit-scrollbar-corner {
  background: $bg_color;
}
.radioMarginRight0 {
  margin-right: 0px;
}
</style>
